class MeetingsController < ApplicationController
  # GET /meetings
  # GET /meetings.xml
  def index
    @meetings = Meeting.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @meetings }
    end
  end

  # GET /meetings/1
  # GET /meetings/1.xml
  def show
    @meeting = Meeting.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @meeting }
    end
  end

  # GET /meetings/new
  # GET /meetings/new.xml
  def new
    @meeting = Meeting.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @meeting }
    end
  end

  # GET /meetings/1/edit
  def edit
    @meeting = Meeting.find(params[:id])
  end

  # POST /meetings
  # POST /meetings.xml
  def create
    @meeting = Meeting.new(params[:meeting])

    respond_to do |format|
      if developer?
        if @meeting.save
          flash[:notice] = 'Meeting was successfully created.'
          format.html { redirect_to(@meeting) }
          format.xml  { render :xml => @meeting, :status => :created, :location => @meeting }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @meeting.errors, :status => :unprocessable_entity }
        end
      else
        format.xml { render :xml => XML_ERRORS[:not_authorized] }
      end
    end
  end

  # PUT /meetings/1
  # PUT /meetings/1.xml
  def update
    @meeting = Meeting.find(params[:id])

    respond_to do |format|
      if developer?
        if @meeting.update_attributes(params[:meeting])
          flash[:notice] = 'Meeting was successfully updated.'
          format.html { redirect_to(@meeting) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @meeting.errors, :status => :unprocessable_entity }
        end
      else
        format.xml { render :xml => XML_ERRORS[:not_authorized] }
      end
    end
  end

  # DELETE /meetings/1
  # DELETE /meetings/1.xml
  def destroy
    @meeting = Meeting.find(params[:id])
    begin
      respond_to do |format|
        @meeting.destroy
        format.html { redirect_to(meetings_url) }
        format.xml  { head :ok }
      end
    rescue ActiveRecord::StatementInvalid 
      respond_to do |format|
        format.xml { render :xml => XML_ERRORS[:foreign_key_problem] }
      end
    end
  end
end
